package abc.red.controller;

import abc.red.common.BaseContext;
import abc.red.entity.D;
import abc.red.entity.R;
import abc.red.entity.Supplier;
import abc.red.service.SupplierService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * @author YiXia
 */
@Slf4j
@RestController
@RequestMapping("/supplier")
public class SupplierController {


    @Autowired
    private SupplierService service;

    /**
     * 设置分页查询的属性
     */
    private int pageSize = 10;

    @PostMapping("/getOneById")
    public R<Supplier> getOneById(@RequestBody D d) {

        Supplier sqlS = service.getById(d.getId());

        //数据肯定存在，直接返回信息
        return R.success(sqlS);
    }

    @PostMapping("/getAllSupplierByPage")
    public R<IPage> getAllByPage(@RequestBody D d) {
        long id = BaseContext.getCurrentId();
        log.info("查询所有供应商信息的操作者id= " + id);
        if (id == 1) {
            //超级管理员可以查询所有
            IPage<Supplier> page = new Page<>(d.getNum1(), pageSize);
            service.page(page);
            return R.success(page);
        } else {
            //普通供应商可以查看自己名下的
            IPage<Supplier> page = new Page<>(d.getNum1(), pageSize);
            LambdaQueryWrapper<Supplier> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Supplier::getManagerId, id);
            service.page(page, lqw);
            return R.success(page);
        }


    }


    @PostMapping("/add")
    public R<String> add(@RequestBody Supplier s) {
        boolean end = service.save(s);
        if (end) {
            log.info("打印SupplierController-add方法上传的Supplier对象数据   {}", s.toString());
            return R.success("新增成功");
        } else {
            return R.error("新增失败");
        }
    }

    @PostMapping("/changeStatus")
    public R<String> changeStatus(@RequestBody D d) {
        log.info("打印参数D " + d.toString());
        Supplier sqlS = service.getById(d.getId());
        sqlS.setStatus((sqlS.getStatus() + 1) % 2);
        //防手贱代码，保护超级管理员账号

        boolean end = service.updateById(sqlS);
        log.info("打印SupplierController-changeStatus方法上传的Supplier对象数据   {}", sqlS.toString());
        if (end) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }


    @PostMapping("/deleteById")
    public R<String> deleteById(@RequestBody Supplier s) {

        boolean end = service.removeById(s);
        if (end) {
            log.info("打印SupplierController-deleteById方法上传的Supplier对象数据   {}", s.toString());
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }


    @PostMapping("/editById")
    public R<String> editById(@RequestBody Supplier s) {
        boolean end = service.updateById(s);
        if (end) {
            log.info("打印SupplierController-editById方法上传的Supplier对象数据   {}", s.toString());
            return R.success("编辑成功");
        } else {
            return R.error("编辑失败");
        }
    }


    //查询名下所有供应商信息

    @PostMapping("/getAllSuppliers")
    public List<Supplier> getAllSuppliers() {

        long id = BaseContext.getCurrentId();
        List<Supplier> list = new ArrayList<>();
        if (id == 1) {
            list = service.list();
        } else {
            LambdaQueryWrapper<Supplier> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Supplier::getManagerId, id);
            list = service.list(lqw);
        }
        log.info("展示查询的所有名下Supplier的信息=   {}",list);
        return list;


    }


}
